package com.physics.mps.common.util;

import com.physics.mps.common.config.constant.Constants;
import com.physics.mps.common.config.properties.ApiProperties;
import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.messages.Bucket;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mcsoft
 * @date 2023/12/15 10:38
 */
@Component
@Log4j2
public class MinioUtil {

    @Resource
    private MinioClient minioClient;
    @Resource
    private ApiProperties apiProperties;

    /**
     * 查看存储bucket是否存在
     *
     * @return boolean
     */
    @SneakyThrows
    public Boolean bucketExists(String bucketName) {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }

    /**
     * 创建存储bucket
     *
     * @return Boolean
     */
    @SneakyThrows
    public void makeBucket(String bucketName) {
        minioClient.makeBucket(MakeBucketArgs.builder()
                .bucket(bucketName)
                .build());
    }

    /**
     * 删除存储bucket
     *
     * @return Boolean
     */
    public Boolean removeBucket(String bucketName) {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 获取全部bucket
     */
    public List<Bucket> getAllBuckets() {
        try {
            List<Bucket> buckets = minioClient.listBuckets();
            return buckets;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 文件上传后返回url
     *
     * @param fileName   文件名
     * @param bucketName 桶名称
     * @param objectName 文件的绝对路径
     * @return
     */
    public ResponseEntity<String> upload(String fileName, String bucketName, String objectName) {
        try {
            // 检查存储桶是否存在
            boolean found = bucketExists(bucketName);
            if (!found) {
                makeBucket(bucketName);
            }

            // 上传文件
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .filename(objectName)
                            .build());

            // 返回 URL
            return ResponseEntity.ok(apiProperties.getDownloadHeader() + apiProperties.getModuleName() + Constants.SLASH + fileName);

        } catch (MinioException e) {
            log.error("上传发生错误：{}", e.getMessage());

            return ResponseEntity.badRequest().body("上传发生错误: " + e.getMessage());
        } catch (Exception e) {
            log.error("未知错误：{}", e.getMessage());

            return ResponseEntity.badRequest().body("未知错误: " + e.getMessage());
        }
    }


}
